package com.nekonomics
package persistence.query

import domain.detail.TopN

import doobie.*
import doobie.implicits.*
import doobie.postgres.implicits.*
import zio.{ULayer, ZLayer}


trait TopNRepo {
  def top10daily(merchantId: Long): ConnectionIO[List[TopN]]
}

object TopNRepo {
  val live: ULayer[TopNRepo] = ZLayer.succeed( TopNRepoLive)
}


object TopNRepoLive extends TopNRepo {
  override def top10daily(merchantId: Long): doobie.ConnectionIO[List[TopN]] = {
    sql"""
         |SELECT * FROM ads.merchant_spu_topn_daily_ads
         |WHERE date_key = CURRENT_DATE AND merchant_id = $merchantId
         |ORDER BY product_rank ASC
         |LIMIT 10
         |""".stripMargin
      .query[TopN]
      .to[List]
  }
}
